package com.huizhonglm.diap.presenters.user;

import com.alibaba.fastjson.JSON;
import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.model.user.MyWalletParams;
import com.huizhonglm.diap.api.model.user.MyWalletResponse;
import com.huizhonglm.diap.api.model.user.OrderInfo;
import com.huizhonglm.diap.api.model.user.OrderListParams;
import com.huizhonglm.diap.core.config.Constants;
import com.huizhonglm.diap.core.utils.UserUtil;
import com.huizhonglm.diap.presenters.BasePresenter;
import com.huizhonglm.diap.ui.user.iView.OrderListIView;

import java.util.List;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-07 13:53
 */
public class OrderListPresenter extends BasePresenter {

    private OrderListIView iView;

    private int pageIndex;

    public OrderListPresenter(OrderListIView iView) {
        this.iView = iView;
    }

    public void loadData(final String pullFlag, String userId, String type){
        switch (pullFlag) {
            case Constants.LOAD_INIT:
            case Constants.LOAD_DROP:
                pageIndex = 1;
                break;
            case Constants.LOAD_MORE:
                pageIndex ++;
                break;
        }
        OrderListParams params = new OrderListParams();
        params.userId = userId;
        params.type = type;
        params.pageNumber = pageIndex;
        API.http().post("orderList", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    List<OrderInfo> data = JSON.parseArray(result, OrderInfo.class);
                    if (!EmptyUtil.isEmpty(data)) {
                        iView.onLoadDataSuccess(pullFlag, data);
                    } else {
                        iView.onLoadDataEmpty(pullFlag);
                    }
                } else {
                    iView.onLoadDataEmpty(pullFlag);
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onLoadDataFailure(pullFlag);
            }
        });
    }

    public void loadMyWallet() {
        MyWalletParams params = new MyWalletParams();
        params.userId = UserUtil.getUserId();
        API.http().post("myWallet", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    MyWalletResponse resp = JSON.parseObject(result, MyWalletResponse.class);
                    if (resp != null) {
                        iView.onLoadMyWalletSuccess(resp);
                    } else {
                        iView.onLoadMyWalletFailure();
                    }
                } else {
                    iView.onLoadMyWalletFailure();
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onLoadMyWalletFailure();
            }
        });
    }
}
